package com.srwl.mytx.widget.chatRow;

import android.content.Intent;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import com.alibaba.fastjson.JSONObject;
import com.srwl.mytx.Constant;
import com.srwl.mytx.domain.Coupon;
import com.srwl.mytx.fragment.RedPacketDialogFragment;
import com.srwl.mytx.utils.DateUtils;
import com.srwl.mytx.utils.Utils;
import com.srwl.mytx.activity.SingleCouponDetailActivity;
import com.srwl.mytx.R;

import java.util.Date;
import java.util.concurrent.TimeUnit;

public class ChatRowRedPacket extends BaseChatRow {

    private TextView mTvGreeting;
    private TextView tv_checkRedPacket;
    private Coupon coupon;

    public ChatRowRedPacket(View itemView) {
        super(itemView);


    }

    @Override
    protected void onInflateView() {


    }

    @Override
    protected void onFindViewById(View itemView) {
        mTvGreeting = itemView.findViewById(R.id.tv_money_greeting);
        tv_checkRedPacket = itemView.findViewById(R.id.tv_check_red_packet);
    }

    @Override
    protected void onSetUpView() {
        coupon = JSONObject.parseObject(message.getContent(), Coupon.class);
        coupon.setTargetUser(targetUser);
        coupon.setSendMessage(isSendMessage);
        String greeting = coupon.getGreeting();
        String filterGreeting = Utils.stringFilter(greeting);
        mTvGreeting.setText(filterGreeting);
        if (coupon.getStatus() != null) {
            switch (coupon.getStatus()) {
                case Constant.COUPON_STATUS_EXPIRED:
                    tv_checkRedPacket.setText(R.string.red_packet_expire);
                    break;
                case Constant.COUPON_STATUS_RECEIVED:
                    tv_checkRedPacket.setText(R.string.red_packet_received);
                    break;
                default:
                    tv_checkRedPacket.setText("查看红包");
            }
        }

        handleTextMessage();
    }

    protected void handleTextMessage() {
        if (isSendMessage) {

            if (this.message.getReadState() == Constant.MESSAGE_READ_STATE_READ) {
                tv_status.setText("已领取");
            } else  {
                Date expiryDate = coupon.getExpiryDate();
                if (DateUtils.isExpired(expiryDate, 0, TimeUnit.HOURS)) {
                    tv_status.setText("已过期");
                } else {
                    tv_status.setText("未领取");
                }
            }

        } else {

        }
    }

    @Override
    protected void onUpdateView() {

    }

    @Override
    protected void onBubbleClick() {
        //如果是接收的红包信息，点击就是打开接收红包对话框，
        if (!isSendMessage) {
            //如果红包已经领取，直接进入红包详情
            if (coupon.getStatus() == Constant.COUPON_STATUS_RECEIVED) {
                activity.startActivity(new Intent(context, SingleCouponDetailActivity.class).putExtra(Constant.EXTRA_INFO_COUPON, coupon));
                return;
            }
            //如果红包未领取或者已经过期，则进入红包对话框
            RedPacketDialogFragment redPacketDialogFragment = RedPacketDialogFragment.newInstance(coupon);
            redPacketDialogFragment.show(activity.getSupportFragmentManager(), "RedPacketDialogFragment");
            redPacketDialogFragment.setOnFragmentInteractionListener(new RedPacketDialogFragment.OnFragmentInteractionListener() {


                @Override
                public void onReceiveCouponComplete(Coupon coupon, boolean isEmpty) {

                    message.setContent(JSONObject.toJSONString(coupon));
                    messageDao.update(message);
                    adapter.notifyItemChanged(getAdapterPosition());
                    if (chatRowCallBack != null) {
                        chatRowCallBack.onMessageRead(message);//让发送方接到回执，红包已领取;普通信息，回执表示-已读/未读，红包信息，回执表示-已领取/未领取
                        Log.d(TAG, "onReceiveCouponComplete: 通知红包已经领取");
                    }
                }


                @Override
                public void onStatusChange(Integer status) {

                }

                @Override
                public void onCouponExpire(Coupon coupon) {
                    message.setContent(JSONObject.toJSONString(coupon));
                    messageDao.update(message);
                    adapter.notifyItemChanged(getAdapterPosition());
                }
            });
        } else {
            //如果是发送方，不管是红包是什么状态，直接进入详情
            activity.startActivity(new Intent(context, SingleCouponDetailActivity.class).putExtra(Constant.EXTRA_INFO_COUPON, coupon));
        }
    }


}
